package com.example.tomatotodo.util;

import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;

import java.util.Arrays;
import java.util.Random;

public class ColorUtils {

    /**
     * 生成随机柔和颜色（用于待办事项卡片背景）
     *
     * @return (int) 随机柔和颜色
     */
    public static int generateRandomPastelColor() {
        Random random = new Random();
        float hue = random.nextInt(360); // 色相
        float saturation = 0.3f; // 低饱和度
        float brightness = 0.95f; // 高亮度
        return Color.HSVToColor(new float[]{hue, saturation, brightness});
    }

    /**
     * 生成随机低饱和度颜色（用于专注界面背景渐变）
     *
     * @return 随机低饱和度颜色数组
     */
    public static int[] generateRandomLowSaturationColors() {
        Random random = new Random();
        int[] colors = new int[3];

        // ---------------------- 颜色相似性控制参数 ----------------------
        // 基础色相（0-360），决定整体色调
        float baseHue = random.nextInt(360);

        // 色相范围控制：值越小，颜色越相似（建议范围：10-60）
        // 例如：10 = 非常相似，30 = 中等相似，60 = 有明显差异但不突兀
        int hueRange = 75;

        // 相邻颜色最小差异：确保有一定变化（建议范围：5-15）
        int minHueDiff = 15;

        // 饱和度范围（0.0-1.0）：值越小颜色越灰，范围越小颜色越相似
        float minSaturation = 0.2f;
        float maxSaturation = 0.4f;  // 缩小范围可增加相似性

        // 亮度范围（0.0-1.0）：值越大越亮，范围越小颜色越相似
        float minBrightness = 0.85f;
        float maxBrightness = 0.95f;  // 缩小范围可增加相似性
        // --------------------------------------------------------------

        // 生成三个在基础色相附近的色相值
        float hue1 = (baseHue - (float) hueRange / 2 + random.nextInt(hueRange)) % 360;
        float hue2 = (baseHue - (float) hueRange / 2 + random.nextInt(hueRange)) % 360;
        float hue3 = (baseHue - (float) hueRange / 2 + random.nextInt(hueRange)) % 360;

        // 确保色相为正值
        if (hue1 < 0) hue1 += 360;
        if (hue2 < 0) hue2 += 360;
        if (hue3 < 0) hue3 += 360;

        // 排序并确保相邻色相至少有最小差异
        float[] hues = {hue1, hue2, hue3};
        Arrays.sort(hues);

        // 检查并调整相邻颜色差异
        for (int i = 1; i < hues.length; i++) {
            while (hues[i] - hues[i - 1] < minHueDiff) {
                hues[i] = (hues[i] + (float) minHueDiff / 2) % 360;
            }
        }

        // 生成略有差异的饱和度
        float saturation1 = minSaturation + random.nextFloat() * (maxSaturation - minSaturation);
        float saturation2 = minSaturation + random.nextFloat() * (maxSaturation - minSaturation);
        float saturation3 = minSaturation + random.nextFloat() * (maxSaturation - minSaturation);

        // 生成略有差异的亮度
        float brightness1 = minBrightness + random.nextFloat() * (maxBrightness - minBrightness);
        float brightness2 = minBrightness + random.nextFloat() * (maxBrightness - minBrightness);
        float brightness3 = minBrightness + random.nextFloat() * (maxBrightness - minBrightness);

        // 生成三个颜色
        colors[0] = Color.HSVToColor(new float[]{hues[0], saturation1, brightness1});
        colors[1] = Color.HSVToColor(new float[]{hues[1], saturation2, brightness2});
        colors[2] = Color.HSVToColor(new float[]{hues[2], saturation3, brightness3});

        return colors;
    }

    /**
     * 获取随机渐变方向
     *
     * @return 随机渐变方向
     */
    public static GradientDrawable.Orientation getRandomOrientation() {
        GradientDrawable.Orientation[] orientations = {
                GradientDrawable.Orientation.LEFT_RIGHT,
                GradientDrawable.Orientation.TOP_BOTTOM,
                GradientDrawable.Orientation.RIGHT_LEFT,
                GradientDrawable.Orientation.BOTTOM_TOP,
                GradientDrawable.Orientation.TR_BL,
                GradientDrawable.Orientation.TL_BR
        };

        Random random = new Random();
        return orientations[random.nextInt(orientations.length)];
    }
}
